package src.main.danli;


import java.io.*;

public class TestSingle {
    public static void main(String[] args) throws IOException, ClassNotFoundException {

        SerializedSingle instanceOne = SerializedSingle.getInstance();

        ObjectOutput out = new ObjectOutputStream(new FileOutputStream(
                "src/main/danli/test_file.txt"));
        out.writeObject(instanceOne);
        out.close();

        //deserailize from file to object
        ObjectInput in = new ObjectInputStream(new FileInputStream(
                "src/main/danli/test_file.txt"));
        SerializedSingle instanceTwo = (SerializedSingle) in.readObject();
        in.close();

        System.out.println("instanceOne hashCode=" + instanceOne.hashCode());
        System.out.println("instanceTwo hashCode=" + instanceTwo.hashCode());

        System.out.println("===============");


//        HungrySingle hungrySingleOne = HungrySingle.getHungrySingle();
//        HungrySingle hungrySingleTwo = null;
//        HungrySingle hungrySingleThree = null;

//        IdlerSingleInnerClass idlerSingleInnerClass1=IdlerSingleInnerClass.getInstance();
//        IdlerSingleInnerClass idlerSingleInnerClass2=null;
//        IdlerSingleInnerClass idlerSingleInnerClass3=null;
//
//        IdlerSingleInner idlerSingleInner1=IdlerSingleInner.getInstance();
//        IdlerSingleInner idlerSingleInner2=null;
//        IdlerSingleInner idlerSingleInner3=null;
//
//        EnumSingle hungrySingleOne = EnumSingle.SINGLE;
//        EnumSingle hungrySingleTwo = null;
//        EnumSingle hungrySingleThree = null;
//
//        try {
////            Constructor[] constructors = HungrySingle.class.getDeclaredConstructors();
//            Constructor[] constructors = EnumSingle.class.getDeclaredConstructors();
//            Constructor[] constructors2 = IdlerSingleInnerClass.class.getDeclaredConstructors();
//            Constructor[] constructors3 = IdlerSingleInner.class.getDeclaredConstructors();
//
////            for (Constructor constructor : constructors) {
////                //Below code will destroy the singleton pattern
////                //关闭了检查
////                constructor.setAccessible(true);
////                hungrySingleTwo = (HungrySingle) constructor.newInstance();
////                hungrySingleThree = (HungrySingle) constructor.newInstance();
////                break;
////            }
//                //枚举无法被反射
//            for (Constructor constructor : constructors) {
//                //Below code will destroy the singleton pattern
//                //关闭了检查
//                constructor.setAccessible(true);
//                hungrySingleTwo = (EnumSingle) constructor.newInstance();
//                hungrySingleThree = (EnumSingle) constructor.newInstance();
//
//                break;
//            }
//
////            for(Constructor constructor : constructors2){
////                constructor.setAccessible(true);
////                idlerSingleInnerClass2 = (IdlerSingleInnerClass) constructor.newInstance();
////                idlerSingleInnerClass3 = (IdlerSingleInnerClass) constructor.newInstance();
////                break;
////            }
//
//            for(Constructor constructor : constructors3){
//                constructor.setAccessible(true);
//                idlerSingleInner2 = (IdlerSingleInner) constructor.newInstance();
//                idlerSingleInner3 = (IdlerSingleInner) constructor.newInstance();
//                break;
//            }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        //饿汉单例能被反射成新对象
//        System.out.println(hungrySingleOne.hashCode());
//        System.out.println(hungrySingleTwo.hashCode());
//        System.out.println(hungrySingleThree.hashCode());
        //内部类不能反射
//        System.out.println(idlerSingleInnerClass1.hashCode());
//        System.out.println(idlerSingleInnerClass2.hashCode());//反射失败
//        System.out.println(idlerSingleInnerClass3.hashCode());//反射失败
        //懒汉加锁能被反射
//        System.out.println(idlerSingleInner1.hashCode());
//        System.out.println(idlerSingleInner2.hashCode());
//        System.out.println(idlerSingleInner3.hashCode());



//        //1.创建可缓存的线程池，可重复利用
//     ExecutorService newExecutorService = Executors.newCachedThreadPool();
//        //创建了10个线程
//        for (int i = 0; i < 10; i++) {
//            int temp = i;
//            newExecutorService.execute(() -> {
////                IdlerSingle idlerSingle=IdlerSingle.getIdlerSingle();
////                System.out.println("懒汉："+idlerSingle);
////                HungrySingle hungrySingle=HungrySingle.getHungrySingle();
////                System.out.println("饿汉："+hungrySingle);
//
////                IdlerSingleInner idlerSingleInner=IdlerSingleInner.getInstance();
////                System.out.println("懒汉加锁："+idlerSingleInner+"----"+IdlerSingleInner.value);
//                EnumSingle enumSingle=EnumSingle.SINGLE;
//                System.out.println("枚举："+enumSingle+"---"+enumSingle.hashCode());
//
//                IdlerSingleInnerClass idlerSingleInnerClass=IdlerSingleInnerClass.getInstance();
//                System.out.println("懒汉内部类："+idlerSingleInnerClass+"----"+IdlerSingleInnerClass.value+"---"+idlerSingleInnerClass.hashCode());
//            });
//        }
//        IdlerSingleInnerClass idlerSingleInnerClass2=IdlerSingleInnerClass.getInstance();
//        System.out.println("懒汉内部类："+idlerSingleInnerClass2+"----"+IdlerSingleInnerClass.value);
//        newExecutorService.shutdown();

//        HungrySingle hungrySingle=HungrySingle.getHungrySingle();
//        System.out.println(hungrySingle);
//        HungrySingle hungrySingle1=HungrySingle.getHungrySingle();
//        System.out.println(hungrySingle1);
//        HungrySingle hungrySingle2=HungrySingle.getHungrySingle();
//        System.out.println(hungrySingle2);

//        RegistrationSingle registrationSingle=(RegistrationSingle)RegistrationSingle.getRegistrationSingle(null);
//        System.out.println(registrationSingle);
//        SQLString registrationSingle2=(SQLString)RegistrationSingle.getRegistrationSingle("utilsString.SQLString");
//        System.out.println(registrationSingle2);
//        SQLString registrationSingle3=(SQLString)RegistrationSingle.getRegistrationSingle("utilsString.SQLString");
//        System.out.println(registrationSingle3);
//        SQLString registrationSingle4=(SQLString)RegistrationSingle.getRegistrationSingle("utilsString.SQLString");
//        System.out.println(registrationSingle4);
//        System.out.println( RegistrationSingle.map.size());


    }
}
